#include<iostream>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int m, n;
		cin >> m >> n;
		int** arr = new int* [m];
		for (int k = 0; k < m; k++) {
			arr[k] = new int[n];
		}

		for (int j = 0; j < m; j++) {
			for (int l = 0; l < n; l++) {
				cin >> arr[j][l];
			}
		}
		int max = arr[0][0];
		int min = arr[0][0];
		for (int j = 0; j < m; j++) {
			for (int l = 0; l < n; l++) {
				if (arr[j][l] > max) {
					max = arr[j][l];
				}
				if (arr[j][l] < min) {
					min = arr[j][l];
				}
			}
		}
		cout << min << ' ' << max << endl;
		for (int p = 0; p < m; p++) {
			delete[] arr[p];
		}
		delete[] arr;
	}
	return 0;
}